package com.twang.stock.task;

import com.twang.stock.Entity.ShiShiData;
import com.twang.stock.common.http.HtmlTools;
import com.twang.stock.entity.CompanyStockMinutes;
import com.twang.stock.entity.TodayTradeAmount;
import com.twang.stock.mapper.CompanyStockMinutesMapper;
import com.twang.stock.mapper.TodayTradeAmountMapper;
import com.twang.stock.task.old.BaseTask;
import com.twang.stock.utils.date.HolidayCopy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 查询昨日成交额前100，今日的表现
 */
@Slf4j
@Service
public class TodayTrade50AmountTask   extends BaseTask {


    @Resource
    private CompanyStockMinutesMapper companyStockMinutesMapper;

    @Resource
    private TodayTradeAmountMapper todayTradeAmountMapper;

    public void excute() throws InterruptedException {
        if (!isWorkTradeTime()) {
            return;
        }
        String date=companyStockMinutesMapper.queryYesterDay();
        excuteQuery("1500", date);
    }

    /**
     *
     */
    public void excuteQuery(String minutes,String date) throws InterruptedException {
        List<CompanyStockMinutes> list  =getTradeAmountDESC(minutes,date);
        if (!CollectionUtils.isEmpty(list)) {
            int sz = 0;
            int xd = 0;
            TodayTradeAmount todayTradeAmount = new TodayTradeAmount();
            for (int i = 0; i < 100; i++) {
                CompanyStockMinutes companyStockMinutes = list.get(i);
                {
                    //获取表单控件值
                    ShiShiData shiShiData = HtmlTools.getGpdatas(Lists.newArrayList(companyStockMinutes.getCompanyCode())).get(0);
                    companyStockMinutes.setZhangDie(shiShiData.getZhangdie().setScale(2, BigDecimal.ROUND_HALF_UP));
                }
                todayTradeAmount.setDate(HolidayCopy.getRealToday());
                todayTradeAmount.setMinutes(HolidayCopy.getHHmmss().substring(8, 12));
                if (companyStockMinutes.getZhangDie().compareTo(BigDecimal.ZERO) == 1) {
                    sz++;
                } else {
                    xd++;
                }
                log.info("companyStockMinutes={}",companyStockMinutes);
            }
            //统计上涨、下跌个数
            todayTradeAmount.setNowSz(sz + "");
            todayTradeAmount.setNowXd(xd + "");
            //统计上涨占比、下跌占比
            todayTradeAmount.setSzzb(new BigDecimal(sz).divide(new BigDecimal(100)).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "%");
            todayTradeAmount.setXdzb(new BigDecimal(xd).divide(new BigDecimal(100)).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "%");

            TodayTradeAmount temp = new TodayTradeAmount();
            temp.setDate(todayTradeAmount.getDate());
            temp.setMinutes(todayTradeAmount.getMinutes());
            if (todayTradeAmountMapper.select(temp).size() > 0) {
                return;
            } else {
                todayTradeAmountMapper.insert(todayTradeAmount);
            }
        }
    }

    /**
     * 查询成交额排前的
     * @param minutes
     * @param date
     * @return
     */
    public List<CompanyStockMinutes> getTradeAmountDESC(String minutes,String date) {
        //查询当日成交额前50
        Example example = new Example(CompanyStockMinutes.class);
        Example.Criteria criteria = example.createCriteria();
        //
        if (StringUtils.isNotBlank(minutes)) {
            criteria.andEqualTo("minutes", minutes);
        } else {
            criteria.andEqualTo("minutes", companyStockMinutesMapper.queryNewMinutes());
        }
        //
        if (StringUtils.isNotBlank(date)) {
            criteria.andEqualTo("date", date);
        } else {
            criteria.andEqualTo("date", companyStockMinutesMapper.queryYesterDayNew());
        }
        example.orderBy("tradeSum").desc();
        List<CompanyStockMinutes> list = companyStockMinutesMapper.selectByExample(example);
        return list;
    }
}
